package com.ruoyi.pay.controller;

import com.ruoyi.pay.utils.ZhifubaoUtill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/alipayExam")
@Slf4j
public class AlipayExamController {

    @Autowired
    private ZhifubaoUtill zhifubaoUtill;

    /**
    * //TODO 支付宝支付（专高六配置文件）
    * @Author: XiXi
    * @Description:
    * @Param: [request, orderId]
    * @return: java.lang.String
    * @Date: 2024/10/25 下午 7:41
    */
    @GetMapping("/pay/{orderId}")
    public String pay(HttpServletRequest request, @PathVariable Integer orderId) throws Exception {
        String html = zhifubaoUtill.pay(orderId+"","0.1").toString();
        return html;
    }


    @GetMapping("/result")
    public String result(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("收到支付结果");
        String orderId = request.getParameter("out_trade_no");
        Boolean result = zhifubaoUtill.queryResult(orderId);
        System.out.println("订单编号" + orderId + "支付结果:" + result);
        System.out.println("修改订单支付状态");
        if (result == true) {
            response.sendRedirect("");
            return "success";
        }else {
            return "fail";
        }

//        System.out.println("收到支付结果");
//        String orderId = request.getParameter("out_trade_no");
//        Boolean result = zhifubaoUtill.queryResult(orderId);
//        System.out.println("订单编号:" + orderId + ",支付结果:" + result);
//        System.out.println("修改订单状态为已支付:" + orderId);
//        if (result == true){
//            //TODO 修改订单状态为已支付
//            response.sendRedirect("https://blog.csdn.net/qq_34886456/article/details/123935048");
//            return "SUCCESS";
//        }else {
//            return "FAIL";
//        }
    }

    /**
    * //TODO 获取支付宝支付信息
    * @Author: XiXi
    * @Description:
    * @Param: [request, orderId]
    * @return: java.lang.Boolean
    * @Date: 2024/10/25 下午 7:47
    */
    @GetMapping("/query/{orderId}")
    public Boolean query(HttpServletRequest request, @PathVariable String orderId) throws Exception {
        Boolean result = zhifubaoUtill.queryResult(orderId);
        return result;
    }

}
